package com.group.eams.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.eams.pojo.dto.ApplicationQueryDto;
import com.group.eams.pojo.dto.ApprovalQueryDto;
import com.group.eams.pojo.dto.AssetClaimQueryDto;
import com.group.eams.pojo.entity.ApplicationList;
import com.group.eams.pojo.entity.Approval;
import com.group.eams.pojo.entity.AssetClaim;
import com.group.eams.pojo.vo.ApplicationListVo;
import com.group.eams.pojo.vo.ApprovalListVo;
import com.group.eams.pojo.vo.AssetClaimListVo;
import com.group.eams.service.ApprovalService;
import com.group.eams.mapper.ApprovalMapper;
import com.group.eams.service.AssetClaimService;
import com.group.eams.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author 恩哲欣
* @description 针对表【approval(审批表)】的数据库操作Service实现
* @createDate 2024-10-29 16:09:34
*/
@Service
public class ApprovalServiceImpl extends ServiceImpl<ApprovalMapper, Approval> implements ApprovalService{

    @Autowired
    private ApprovalMapper approvalMapper;

    /**
     * 根据查询条件分页查询申请列表
     *
     * @param applicationQueryDto 查询条件
     * @return 申请列表
     */
    @Autowired
    private AssetClaimService assetClaimService;
    @Autowired
    private UserService userService;
    @Override
    public Page<ApplicationListVo> findApplicationList(ApplicationQueryDto applicationQueryDto) {
        // 创建分页对象
        List<ApplicationListVo> applicationList = new ArrayList<>();
        List<AssetClaim> list = assetClaimService.list();
        for (AssetClaim assetClaim : list){
            ApplicationListVo applicationListVo = new ApplicationListVo();
            applicationListVo.setApplicationNum(assetClaim.getApplicationNum());
            applicationListVo.setTitle(assetClaim.getTitle());
            applicationListVo.setApplicationDate(assetClaim.getApplicationDate());
            applicationListVo.setApplicationType(1);
            applicationListVo.setApplicant(assetClaim.getApplicantId() == null ? null : userService.getById(assetClaim.getApplicantId()).getName());
            applicationListVo.setStatus(assetClaim.getState());
            applicationListVo.setCreator(assetClaim.getCreatorId() == null ? null : userService.getById(assetClaim.getCreatorId()).getName());
            applicationListVo.setCreated(assetClaim.getCreated());
            applicationList.add(applicationListVo);
        }
//        for (AssetClaimListVo claimListVo : claim.getRecords()) {
//            ApplicationListVo applicationListVo = ApplicationList.getApplicationListVo(claimListVo);
//            applicationList.add(applicationListVo);
//        }


        Stream<ApplicationListVo> stream = applicationList.stream();
        //        stream流设置查询条件
        if (!StrUtil.isEmpty(applicationQueryDto.getApplicationNum())){
            stream = stream.filter(applicationListVo -> applicationListVo.getApplicationNum().contains(applicationQueryDto.getApplicationNum()));
        }
        if (!StrUtil.isEmpty(applicationQueryDto.getTitle())){
            stream = stream.filter(applicationListVo -> applicationListVo.getTitle().contains(applicationQueryDto.getTitle()));
        }
        if (applicationQueryDto.getApplicationType()!=null){
            stream = stream.filter(applicationListVo -> applicationListVo.getApplicationType().equals(applicationQueryDto.getApplicationType()));
        }
        if (applicationQueryDto.getApplicationDate()!=null){
            stream = stream.filter(applicationListVo -> applicationListVo.getApplicationDate().equals(applicationQueryDto.getApplicationDate()));
        }
        if (applicationQueryDto.getStatus()!=null){
            stream = stream.filter(applicationListVo -> applicationListVo.getStatus().equals(applicationQueryDto.getStatus()));
        }
        List<ApplicationListVo> records  = stream.collect(Collectors.toList());
        Page<ApplicationListVo> page = new Page<>(applicationQueryDto.getPage(), applicationQueryDto.getLimit());
        return page.setRecords(records);
    }

    /**
     * 根据查询条件分页查询审批列表
     *
     * @param approvalQueryDto 查询条件
     * @return 审批列表
     */
    @Override
    public Page<ApprovalListVo> findByQueryDto(ApprovalQueryDto approvalQueryDto) {
        Page<ApprovalListVo> page = new Page<>(approvalQueryDto.getPage(), approvalQueryDto.getLimit());
        return approvalMapper.findByQueryDto(page, approvalQueryDto);
    }
}
